<?php

class DiaryTopic
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 20;
    
    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getDiaryWithComments','displayTopicDiary','removeDiary','getJoinNum','getSquareList','getDiaryAllInfo','searchDiary','hotReady','addHotDiaryData','getDiaryDetail','hotDiary','getDiaryCommentsOfAjax',
            'setHotDiaryData','getUserDiaryList','delHotDiaryReadyByBodyId','delHotDiaryByBodyId','delUserComment','deletechildComment'
        );
    }
    
    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;
        
        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    /**
     * 热门待上线列表
     * @param $req
     * @return string
     * @throws Exception
     */
    private function hotReady($req) {
        $req['page'] = $req['page'] ? abs(intval($req['page'])) : 1;
        $req['type'] = $req['type'] ? $req['type'] : 'all';
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);

        $num = intval(($req['page'] - 1) * $this->length + 1);
        $req['totalPage'] = ceil($res['counts'] / $this->length);
        $pageUrl = $this->pageUrl($req);

        return render('diary_list', array('list' => $res['status'], 'pageUrl' => $pageUrl, 'num' => $num, 'type' => $req['type']));
    }
    /**
     * 热门已上线列表
     * @param $req
     * @return string
     * @throws Exception
     */
    private function hotDiary($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $req['bodyId'] = 0;
        if ($res) $req['bodyId'] = $res['status'][count($res['status']) -1]['bodyId'];
        $num = ceil($req['start'] / $this->length) * $this->length + 1;
        $pageUrl = $this->pageUrl($req);
        return render('hotDiary_list', array('list' => $res['status'] , 'bodyId' => $req['bodyId'] ,  'pageUrl' => $pageUrl , 'num' => $num ));
    }
    /**
     * 添加数据到待上线列表
     * @param $req
     */
    private function addHotDiaryData ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    private function setHotDiaryData ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    private function getJoinNum($req) {
		$param = $this->filterArgumentsOfPassed($req);
		$res = requestApi($param);
		header('Content-Type: application/json; charset=utf-8');
		$result = json_encode($res);
		echo $result;
		exit;
	}
    /**
     * 获取点滴详情
     * @param unknown $req
     * @return unknown
     */
    private function getDiaryWithComments ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res) || empty($res['listInfo']['bodyId'])) return false;

        return '开发中';
        return render('diarymodal', $res);
    }

    /**
     * 获取用户点滴列表
     * @param unknown $req
     * @return unknown
     */
    private function getUserDiaryList ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $req['id'] = 0;
        if ($res) $req['id'] = $res[count($res) -1]['id'];
        $req['uid'] = $param['uid'];
        $pageUrl = $this->pageUrl($req);
        return render('userDiaryList', array('list'=>$res, 'pageUrl' => $pageUrl));
    }

    /**
     * 新鲜列表
     * @param $req
     * @return string
     */
    private function getSquareList($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $req['bodyId'] = 0;
        if ($res) $req['bodyId'] = $res[count($res) -1]['bodyId'];
        $pageUrl = $this->pageUrl($req);
        return render('squareList', array('list' => $res, 'bodyId' => $req['bodyId'], 'pageUrl' => $pageUrl));
    }

    /**
     * 点滴文章详情
     * @param $req
     * @return string
     */
    private function getDiaryAllInfo($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        return render('diaryAllInfo', array('list' => $res));
    }

    /**点滴详情
     * @param $req
     * @return string
     */
    private function getDiaryDetail($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $comments = $res['commentInfo']['comments'];

        $result = array(
            'listInfo' => $res['listInfo'] ? $res['listInfo'] : array(),
            'bodyInfo' => $res['bodyInfo'] ? $res['bodyInfo'] : array(),
            'attachment' => $res['attachment'] ? $res['attachment'] : array(),
            'voiceList' => $res['voiceList'] ? $res['voiceList'] : array(),
            'user' => $res['user'] ? $res['user'] : array(),
            'comments' => $res['commentInfo']['comments'] ? $res['commentInfo']['comments'] : array(),
            'followInfo' => $res['followInfo'] ? $res['followInfo'] : array(),
            'tid' => $res['listInfo']['bodyId'] ? $res['listInfo']['bodyId'] : array(),
            'id' => $comments[count($comments) - 1]['id'] ? $comments[count($comments) - 1]['id'] : 0,
            'author_uid' => $res['user']['uid'] ? $res['user']['uid'] : 0,
        );
        return render('diary_detail_new', $result);
    }

    /**
     * 获取下页评论数据
     * @param $req
     * @return string
     * @throws Exception
     */
    private function getDiaryCommentsOfAjax ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $comments = $res['comments'];
        $result = array('comments' => $comments, 'id' => $comments[count($comments) - 1]['id']);
        return render('diary_comment_loop', $result);
    }


    /**
     * 删除评论数据
     * @param $req
     * @return string
     * @throws Exception
     */
    private function delUserComment($req)
    {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        } else {
            echo 1;
        }
    }

    /**
     * 删除楼中楼的评论
     * @param $req
     * @return string
     * @throws Exception
     */
    private function deletechildComment ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if($res){
            echo 1;
        } else {
            echo 0;
        }
    }

    /**
     * 搜索
     * @param unknown $req
     * @return unknown
     */
    private function searchDiary ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        return render('fetchDiaryInfo', array('list' => $res));
    }

    /**
     * 删除点滴
     * @param unknown $req
     * @return unknown
     */
    private function removeDiary ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $res = requestApi($param);
        if($res)
        {
            echo 1;
        }else{
            echo 0;
        }
    }

    /**
     * 删除热门点滴待上线
     * @param unknown $req
     * @return unknown
     */
    private function delHotDiaryReadyByBodyId ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    /**
     * 批量删除热门点滴已上线
     * @param unknown $req
     * @return unknown
     */
    private function delHotDiaryByBodyId ($req) {
        $bodyIds = explode(',',$req['bodyId']);
        for($i=0;$i<count($bodyIds);$i++)
        {
            $req['bodyId'] = $bodyIds[$i];
            $param = $this->filterArgumentsOfPassed($req);
            $res = requestApi($param);
        }
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    /**
     * 将点滴置顶到某个话题下面
     * @param unknown $req
     * @return unknown
     */
    private function displayTopicDiary ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        
        $res = requestApi($param);
        
        return $res;
    }
    
    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getDiaryWithComments':
                $param = array(
                    'do'     => 'getDiaryWithComments',
                    'format' => 'json',
                    'uid'    => intval($req['uid']),
                    'bodyId' => intval($req['bodyId']),
                );
                break;
            case 'removeDiary':
                $param = array(
                    'do'     	=> 'removeDiary',
                    'format' 	=> 'json',
                    'bodyId'  	=> intval($req['bodyId']),
                    'uid'		=> intval($req['uid'])
                );
                break;
            case 'delHotDiaryReadyByBodyId':
                $param = array(
                    'do'     	=> 'delHotDiaryReadyByBodyId',
                    'format' 	=> 'json',
                    'bodyId'  	=> intval($req['bodyId']),
                    'onlineTime'  => !empty($req['onlineTime']) ? $req['onlineTime'] : 'all'
                );
                break;
            case 'displayTopicDiary':
                $param = array(
                    'do'      => 'displayTopicDiary',
                    'format'  => 'json',
                    'uid'     => trim($req['uid']),
                    'bodyId'  => trim($req['bodyId']),
                    'topic_id'=> trim($req['gid']),
                    'display' => 1,
                    'revert'  => empty($req['revert']) ? 0 : 1
                );
                break;
			case 'getJoinNum':
				$param = array(
					'do'     => 'getJoinNum',
					'format' => 'json',
					'gid'  	 => intval($req['gid']),
				);
				break;
            case 'getSquareList':
                $param = array(
                    'do'     => 'getSquareList',
                    'format' => 'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                    'id'     => empty($req['bodyId']) ? 0 : $req['bodyId'],
                );
                break;
            case 'getDiaryAllInfo':
                $param = array(
                    'do'     => 'getDiaryAllInfo',
                    'format' => 'json',
                    'uid' 	 => intval($req['uid']),
                    'bodyId' => intval($req['bodyId']),
                );
                break;
            case 'getDiaryDetail':
                $param = array(
                    'do'     => 'getDiaryWithComments',
                    'format' => 'json',
                    'uid' 	 => intval($req['uid']),
                    'bodyId' => intval($req['bodyId']),
                );
                break;
            case 'searchDiary':
                $param = array(
                    'do'     => 'searchDiary',
                    'format' => 'json',
                    'uid' 	 => intval($req['uid']),
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' =>  empty($req['length']) ? $this->length : $req['length'],
                    'title'  => empty($req['title']) ? $req['title'] : "%".$req['title']."%",
                    'nickname'  => empty($req['nickname']) ? $req['nickname'] : "%".$req['nickname']."%",

                );
                break;
	        case 'hotReady':
                $param = array(
                    'do'     	=> 'getHotDiaryReady',
                    'format' 	=> 'json',
                    'start'  	=> (intval($req['page']) - 1) * $this->length,
                    'length'    => $this->length,
                    'type'      => $req['type']
                );
                break;
            case 'hotDiary':
                $param = array(
                    'do'        =>  'getHotDiary',
                    'format'    =>  'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                    'id'     => empty($req['bodyId']) ? 0 : $req['bodyId'],

            );
                break;
            case 'addHotDiaryData':
                $param = array(
                    'do'     => 'setHotDiaryData',
                    'format' => 'json',
                    'bodyId' => intval($req['bodyId']) ? intval($req['bodyId']) : 0,
                    //权重默认为200
                    'score'  => intval($req['score']) ? intval($req['score']) : 200,
                    'onlineTime'  => $req['onlineTime']
                );
                break;
            case 'setHotDiaryData':
                $param = array(
                    'do'     => 'setHotDiaryData',
                    'format' => 'json',
                    'bodyId' => intval($req['bodyId']) ? intval($req['bodyId']) : 0,
                    'score'  => intval($req['score']) ? intval($req['score']) : 0,
                    'onlineTime'  => !empty($req['onlineTime']) ? $req['onlineTime'] : 'all'
                );
                break;
            case 'getDiaryCommentsOfAjax':
                $param = array(
                    'do'        =>  'comment',
                    'format'    =>  'json',
                    'tid'       =>  intval($req['tid']),
                    'author_uid'=>  intval($req['author_uid']),
                    'id'        =>  intval($req['id']),
                    'position'  =>  isset($req['position']) ? intval($req['position']) : 0,
                    'uid'        =>  intval($req['author_uid']),
                    'length'    =>  $this->length,
                    'action'    =>  'getDiaryComments'
                );
                break;
            case 'getUserDiaryList':
                $param = array(
                    'do'     => 'getUserDiaryList',
                    'format' => 'json',
                    'uid'    => isset($req['uid']) ? $req['uid'] : $_SESSION['user']['adminUid'],
                    'length' => empty($req['length']) ? $this->length : intval($req['length']),
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'id'     => empty($req['id']) ? 0 : $req['id'],
                    'secret' => 0
                );
                break;
            case 'delHotDiaryByBodyId':
                $param = array(
                    'do'     => 'delHotDiaryReadyByBodyId',
                    'format' => 'json',
                    'bodyId' => intval($req['bodyId']),
                    'onlineTime'  => !empty($req['onlineTime']) ? $req['onlineTime'] : 'all'
                );
                break;
            case 'delUserComment':
                $param = array(
                    'do'         => 'postComments',
                    'format'     => 'json',
                    'tid'        => intval($req['tid']),
                    'position'   => intval($req['position']),
                    'author_uid' => intval($req['author_uid']),
                    'uid'        => intval($req['uid']),
                    'action'     => 'removeUserComment',
                );
                break;
            case 'deletechildComment':
                $param = array(
                    'do'         => 'postComments',
                    'format'     => 'json',
                    'tid'        => intval($req['tid']),
                    'position'   => intval($req['position']),
                    'positionId' => isset($req['positionId']) ? $req['positionId'] : '0',
                    'author_uid' => intval($req['author_uid']),
                    'uid'        => intval($req['uid']),
                    'action'     => 'removeDiaryChildComment',
                );
                break;
        }
        
        return $param;
    }

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "getSquareList":
                $mpurl .= "&bodyId={$req['bodyId']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
           case "hotReady":
               $mpurl .= "&type={$req['type']}";
               $pageurl = multitotal($req['page'], $req['totalPage'], $mpurl);
               break;
            case "hotDiary":
                $mpurl .= "&bodyId={$req['bodyId']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
	    case "getUserDiaryList":
                $mpurl .= "&id={$req['id']}&uid={$req['uid']}";
		        $pageurl = multi($req['start'], $this->length, $mpurl);
                break;	
        }

        return $pageurl;
    }
    
}

$req = request();
$diaryTopic   = new DiaryTopic($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);

